package com.zzyl.nursing.service.impl;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.*;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.UserThreadLocal;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.ReservationVo;
import net.sf.jsqlparser.expression.operators.relational.Between;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 预约信息Service业务层处理
 * 
 * @author zhaoyiyang
 * @date 2025-07-31
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
//        分页查询
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertReservation(Reservation reservation)
    {
        reservation.setType(0);
        reservation.setStatus(0);
        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public AjaxResult countByTime(Long time) {
        /*
        * 业务分析:
        * 1.我们要查询当天预约信息,如果当前时间段没有人预约则不返回,表示可以预约6次
        * 2.如果当前时间段有人预约,则可预约的次数为6-已预约人数,并返回当前的时间段
        * */
//        1.查询对应当天的预约信息
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        List<ReservationVo> reservationVos = reservationMapper.countByTime(startTime, endTime);
        return AjaxResult.success(reservationVos);
    }

    @Override
    public AjaxResult findCancelledCount() {
//        查询当天取消预约的数量
        /*
        * 首先:
        * 1.我们要查询当天时间内status=2的预约信息
        * 2.然后:count聚合函数查询总条数
        * 3.最后:返回结果
        * */
//        获取当前线程中的用户
        Long userId = UserThreadLocal.getUserId();
        FamilyMember familyMember = familyMemberMapper.selectOne(Wrappers.<FamilyMember>lambdaQuery().eq(FamilyMember::getId, userId));
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        long count = this.count(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getName,familyMember.getName())
                .eq(Reservation::getStatus, 2)
                .between(Reservation::getCreateTime,startTime,endTime)
        );
        return AjaxResult.success(count);
    }

    @Transactional
    @Override
    public AjaxResult cancelReservation(Long id) {
        this.update(Wrappers.<Reservation>lambdaUpdate()
                .eq(Reservation::getId,id)
                .set(Reservation::getStatus,2)
        );
        return AjaxResult.success();
    }
}
